﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class masterdata_Form_Maintain : PageBase
{
    private int id = 0;
    private Jumpton.Persistent.ObjectTable ot;
    private const string FORM_ID = "id";
    private const string RESPONSE_FORM_LIST = "Form_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FORM_ID] != null)
        {
            int.TryParse(Request.QueryString[FORM_ID], out id);
        }

        if (!IsPostBack)
        {
            InitForm();
            FillForm();
        }
    }

    private void FillForm()
    {
        if (id > 0)
        {
            FormManager manager = new FormManager();
            FORMS fm = manager.Load(id);
            ddlBrand.SelectedValue = fm.BrandCode;
            ddlCategroy.SelectedValue = fm.CategorieCode;
            ddlManu.SelectedValue = fm.ManufacturerCode;
            txtCode.Text = fm.FormCode;
            txtName.Value = fm.FormName;
            txtCode.Enabled = false;
            chkValid.Checked = fm.ACTIVE;
        }
    }

    private void InitForm()
    {
        GetManuType();
        GetCategoryType();
        GetBrandType();
    }

    #region 绑定品牌
    public void GetBrandType()
    {
        ddlBrand.Items.Clear();
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        ot = BrandManager.GetBrands(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlBrand.DataSource = ot.BillDataTable.DefaultView;
            ddlBrand.DataBind();
        }
        ddlBrand.Items.Insert(0, new ListItem("-选择品牌-", ""));
    }
    #endregion

    #region 绑定制造商
    public void GetManuType()
    {
        ddlManu.DataTextField = "ManuName";
        ddlManu.DataValueField = "ManuCode";
        ot = ManufacturersManager.GetManus(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlManu.DataSource = ot.BillDataTable.DefaultView;
            ddlManu.DataBind();
        }
        ddlManu.Items.Insert(0, new ListItem("-选择制造商-", ""));
    }
    #endregion

    #region 绑定品类
    public void GetCategoryType()
    {

        ddlCategroy.DataTextField = "CategoryName";
        ddlCategroy.DataValueField = "CategoryCode";
        ot = CategoryManager.GetCategories(MyStateManager.DomainID);
        if (ot != null)
        {
            ot.BillDataTable.DefaultView.RowFilter = "ACTIVE=1";
            ddlCategroy.DataSource = ot.BillDataTable.DefaultView;
            ddlCategroy.DataBind();
        }
        ddlCategroy.Items.Insert(0, new ListItem("-选择品类-", ""));
    }
    #endregion

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {

            try
            {

                FORMS fm = null;
                FormManager fmManager = new FormManager();
                if (id == 0)
                {
                    fm = new FORMS();
                    fm.FormCode = txtCode.Text.Trim();
                }
                else
                {
                    fm = fmManager.Load(id);
                }

                fm.DOMAINID = MyStateManager.DomainID;
                fm.FormName = txtName.Value.Trim();
                fm.BrandCode = ddlBrand.SelectedValue;
                fm.ManufacturerCode = ddlManu.SelectedValue;
                fm.CategorieCode = ddlCategroy.SelectedValue;
                fm.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                fm.LAST_UPDATER = MyStateManager.UserID.ToString();
                fm.ACTIVE = chkValid.Checked;

                string LogContent = "";

                if (fm.State == DataRowState.Added)
                {
                    LogContent += "增加新的系列" + txtCode.Text.Trim() + "，它的信息是：";
                }
                else
                {
                    LogContent += fm.FormCode + "信息发生变动，变动后信息是：";
                }
                LogContent += "系列代码：" + fm.FormCode + "，";
                LogContent += "系列名称：" + fm.FormName + "，";
                LogContent += "制造商：" + ddlManu.SelectedItem.Text + "，";
                LogContent += "品类名称：" + ddlCategroy.SelectedItem.Text + "，";
                LogContent += "品牌名称：" + ddlBrand.SelectedItem.Text + "，";
                LogContent += "有效：" + fm.ACTIVE + "，";
                LogContent += "操作人：" + fm.LAST_UPDATER;


                bool Successful = fmManager.Save(fm);

                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                 Jumpton.SFA.Server.BusinessRule.System.LogType.Info_Change, this.MyStateManager.Name,
                 this.MyStateManager.UserID, LogContent);

                ShowMessage("保存成功", RESPONSE_FORM_LIST);
            }
            catch (Exception ex)
            {
                Jumpton.SFA.Server.BusinessRule.System.EventManager.CreateNewEvent(
                      Jumpton.SFA.Server.BusinessRule.System.LogType.GENERAL_EXCEPTION, this.MyStateManager.Name,
                      this.MyStateManager.UserID, "系列维护 - 异常:" + ex.Message);
                
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

   

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RESPONSE_FORM_LIST);
    }
    protected void ddlManu_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlBrand.DataTextField = "BrandName";
        ddlBrand.DataValueField = "BrandCode";
        DataSet ds = BrandManager.GetBrandsListDaynamic(ddlCategroy.SelectedValue, ddlManu.SelectedValue, MyStateManager.DomainID);
        ddlBrand.DataSource = ds;
        ddlBrand.DataBind();
        ddlBrand.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));
    }
}
